% Move Results to Permanent Storage 
method_names={'VAR','LP','Pool','Poolfc','BLP'};
n_methods=5;

results.irf.(method_names{1})=IRF_VAR_result;
results.irf.(method_names{2})=IRF_LP_result;
results.irf.(method_names{3})=IRF_Pool_result;
results.irf.(method_names{4})=IRF_Pool_fc_result;
results.irf.(method_names{5})=IRF_BLP_result;

the_true_irf = DF_model.target_irf;         % True IRF
the_rms_irf  = sqrt(mean(the_true_irf.^2)); % Root average squared true IRF across horizons
        
% Compute Mean-Squared Errors, Bias-Squared, Variance
for i_method = 1:n_methods    
    thisMethod = method_names{i_method};    
   results.MSE.(thisMethod) = squeeze(mean((results.irf.(thisMethod) - permute(DF_model.target_irf,[1 3 2])).^2, 2));    
   results.BIAS2.(thisMethod) = (squeeze(mean(results.irf.(thisMethod), 2)) - DF_model.target_irf).^2;    
   results.VCE.(thisMethod) = squeeze(var(results.irf.(thisMethod), 0, 2));
end


%Criteria results
     RMSE_result= sqrt(extract_struct(results.MSE));
     RMSE_result_plot=squeeze(mean(RMSE_result./the_rms_irf, 2));
    
     BIAS_result=sqrt(extract_struct(results.BIAS2));
     BIAS_result_plot=squeeze(mean(BIAS_result./the_rms_irf, 2));
     
     STD_result=sqrt(extract_struct(results.VCE));
     STD_result_plot=squeeze(mean(STD_result./the_rms_irf, 2));

% Estimate Coverage Rage and Average Length
% Average length
IRF_VAR_seqblock_avglength=mean(IRF_VAR_seqblock_length,2);
IRF_LP_seqblock_avglength=mean(IRF_LP_seqblock_length,2);
IRF_Pool_seqblock_avglength=mean(IRF_Pool_seqblock_length,2);
IRF_Pool_seqblock_avglength_bs1=mean(IRF_Pool_seqblock_length_bs1,2);
IRF_Pool_seqblock_avglength_bs2=mean(IRF_Pool_seqblock_length_bs2,2);

IRF_VAR_bootrep_avglength=mean(IRF_VAR_bootrep_length,2);
IRF_LP_bootrep_avglength=mean(IRF_LP_bootrep_length,2);
IRF_Pool_bootrep_avglength=mean(IRF_Pool_bootrep_length,2);

% Average length across all specifications
IRF_VAR_seqblock_avglength_acrossspec=mean(IRF_VAR_seqblock_avglength,3);
IRF_LP_seqblock_avglength_acrossspec=mean(IRF_LP_seqblock_avglength,3);
IRF_Pool_seqblock_avglength_acrossspec=mean(IRF_Pool_seqblock_avglength,3);
IRF_Pool_seqblock_avglength_acrossspec_bs1=mean(IRF_Pool_seqblock_avglength_bs1,3);
IRF_Pool_seqblock_avglength_acrossspec_bs2=mean(IRF_Pool_seqblock_avglength_bs2,3);

IRF_VAR_bootrep_avglength_acrossspec=mean(IRF_VAR_bootrep_avglength,3);
IRF_LP_bootrep_avglength_acrossspec=mean(IRF_LP_bootrep_avglength,3);
IRF_Pool_bootrep_avglength_acrossspec=mean(IRF_Pool_bootrep_avglength,3);

% Coverage rate
CVG_var_seqblock=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_var_bootrep=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_lp_seqblock=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_lp_bootrep=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_pool_seqblock=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_pool_seqblock_bs1=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_pool_seqblock_bs2=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);
CVG_pool_bootrep=zeros(IRF_hor,settings.simul.n_MC,settings.specifications.n_spec);

for i_spec=1:settings.specifications.n_spec  
    for i_MC = 1:settings.simul.n_MC  
        for j=1:IRF_hor

            if  (DF_model.target_irf(j,i_spec)>=IRF_VAR_LB_seqblock(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_VAR_UB_seqblock(j,i_MC,i_spec))
                CVG_var_seqblock(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_VAR_LB_bootrep(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_VAR_UB_bootrep(j,i_MC,i_spec))
                CVG_var_bootrep(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_LP_LB_seqblock(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_LP_UB_seqblock(j,i_MC,i_spec))
                CVG_lp_seqblock(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_LP_LB_bootrep(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_LP_UB_bootrep(j,i_MC,i_spec))
                CVG_lp_bootrep(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_Pool_LB_seqblock(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_Pool_UB_seqblock(j,i_MC,i_spec))
                CVG_pool_seqblock(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_Pool_LB_seqblock_bs1(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_Pool_UB_seqblock_bs1(j,i_MC,i_spec))
                CVG_pool_seqblock_bs1(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_Pool_LB_seqblock_bs2(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_Pool_UB_seqblock_bs2(j,i_MC,i_spec))
                CVG_pool_seqblock_bs2(j,i_MC,i_spec) = 1;
            end

            if  (DF_model.target_irf(j,i_spec)>=IRF_Pool_LB_bootrep(j,i_MC,i_spec)) && (DF_model.target_irf(j,i_spec)<=IRF_Pool_UB_bootrep(j,i_MC,i_spec))
                CVG_pool_bootrep(j,i_MC,i_spec) = 1;
            end
            
                   
        end
          
    end
end
% Average Coverage
CVG_var_seqblock_avg=sum(CVG_var_seqblock,2)/settings.simul.n_MC;
CVG_var_bootrep_avg=sum(CVG_var_bootrep,2)/settings.simul.n_MC;
CVG_lp_seqblock_avg=sum(CVG_lp_seqblock,2)/settings.simul.n_MC;
CVG_lp_bootrep_avg=sum(CVG_lp_bootrep,2)/settings.simul.n_MC;
CVG_pool_seqblock_avg=sum(CVG_pool_seqblock,2)/settings.simul.n_MC;
CVG_pool_seqblock_avg_bs1=sum(CVG_pool_seqblock_bs1,2)/settings.simul.n_MC;
CVG_pool_seqblock_avg_bs2=sum(CVG_pool_seqblock_bs2,2)/settings.simul.n_MC;
CVG_pool_bootrep_avg=sum(CVG_pool_bootrep,2)/settings.simul.n_MC;

% Average Coverage across all specifications
CVG_var_seqblock_avg_across=mean(CVG_var_seqblock_avg,3);
CVG_var_bootrep_avg_across=mean(CVG_var_bootrep_avg,3);
CVG_lp_seqblock_avg_across=mean(CVG_lp_seqblock_avg,3);
CVG_lp_bootrep_avg_across=mean(CVG_lp_bootrep_avg,3);
CVG_pool_seqblock_avg_across=mean(CVG_pool_seqblock_avg,3);
CVG_pool_seqblock_avg_across_bs1=mean(CVG_pool_seqblock_avg_bs1,3);
CVG_pool_seqblock_avg_across_bs2=mean(CVG_pool_seqblock_avg_bs2,3);
CVG_pool_bootrep_avg_across=mean(CVG_pool_bootrep_avg,3);   

% Average correlation
correlation_varlp_seqblock_save=mean(correlation_varlp_seqblock_sim,4);
correlation_varlp_seqblock_save_bs1=mean(correlation_varlp_seqblock_sim_bs1,4);
correlation_varlp_seqblock_save_bs2=mean(correlation_varlp_seqblock_sim_bs2,4);
correlation_varlp_bootrep_save=mean(correlation_varlp_bootrep_sim,4);
